package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.api.OrderProductService;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;


@Slf4j
@Component
public class ClearCartItemHandler extends AbstractTransHandler {


    @Autowired
    OrderProductService orderProductService;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderMapper orderMapper;

    //是否采用异步方式执行
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // 删除购物车商品，调用商品服务
        // context向上转通过orderId到item&product表里查出productlist
        AbsTransHandlerContext absContext = (AbsTransHandlerContext) context;

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(absContext.getOrderId());
        Order order = orderMapper.selectByPrimaryKey(absContext.getOrderId());
        List<Long> productIds = new LinkedList<>();
        // 封装orderId列表
        for (OrderItem orderItem : orderItems) {
            productIds.add(orderItem.getItemId());
        }

        // 封装成request(记得补上convertor)
        ClearCartItemRequest clearCartItemRequest = new ClearCartItemRequest();
        clearCartItemRequest.setUserId(order.getUserId());
        clearCartItemRequest.setProductIds(productIds);

        // 封装下面方法的传参


        orderProductService.clearCartItemByUserID(clearCartItemRequest);

        return true;
    }
}
